require('./PageAchievementmanage.styl');

const reactMixin = require('react-mixin');
const Actions = require('./actions');
const Store = require('./store');
const { Group, TextField, SelectField, DatetimeField, Button, Boxs, Toast } = SaltUI;
const { HBox, Box } = Boxs;
import Addpersonfield from 'components/addpersonfield'
import { mergeArray } from 'clientConfig/util/queryurlfield';
import { Control } from 'react-keeper';
const Fileselect = require('components/fileselect')
const nodeList = {
    software: [
        { value: 0, text: '方案评审' },
        { value: 1, text: '项目实施/开发' },
        { value: 2, text: '内部测试' },
        { value: 3, text: '第三方测试' },
        { value: 4, text: '项目初验' },
        { value: 5, text: '试运行' },
        { value: 6, text: '项目终验' }]
}
class Achievementmanage extends React.Component {

    constructor(props) {
        super(props);
        if (props.pagedata) {
            let nodelists = props.pagedata.node_info;
            var path_name = JSON.parse(nodelists[nodelists.length - 1].value)[0].selected_folder_pathname;
        }
        this.state = {
            upfileName: [],//文件上传
            approvers: props.pagedata && props.pagedata.approvers || [],//审批人姓名列表
            nodeList: [],//项目所属节点列表
            proNumAll: [],//所有的项目编号
            proNameAll: [],//所有的项目名称
            copyers: [],//审批抄送人姓名列表
            options: [{ value: 0, text: '是' },
            { value: 1, text: '否' }],//是否初次提交
            achieveName: '',
            finishTime: '',
            initSubmit: '',
            proName: '',
            proTime: '',
            node: '',
            path_name: '' || props.pagedata && path_name
        };
        this.data = [
            { label: "成果名称", name: "achieveName" },
            { label: "成果完成时间", name: "finishTime" },
            { label: "所属节点", name: "node" },
            { label: "是否初次提交", name: "initSubmit" },
        ]
        if (props.pagedata) {
            let nodelists = props.pagedata.node_info;
            for (let i = 0; i < this.data.length; i++) {
                if (this.data[i].name) {
                    if (i == 1) {
                        this.state[this.data[i].name] = new Date(nodelists[i].value).getTime()
                    } else if (i == 3) {
                        this.state[this.data[i].name] = { value: nodelists[i].value == "是" ? 0 : 1, text: nodelists[i].value }//是否初次提交
                    } else if (i == 2) {
                        let n = 0;
                        for (let key in nodeList.software) {
                            if (nodeList.software[key].text == nodelists[i].value) {
                                n = key;
                            }
                        }
                        this.state[this.data[i].name] = nodeList.software[n]
                    } else {
                        this.state[this.data[i].name] = nodelists[i].value;
                    }
                }
            }
            this.state.proName = props.pagedata.proName
            this.state.proNum = props.pagedata.proNum
            this.state.upfileName = JSON.parse(nodelists[nodelists.length - 1].value)
            this.state.folder_id = this.state.upfileName[0].root_folder_Id;
            this.state.edit = props.edit;
        }
        this.setNoticeInfo();
    }
    //设置提示信息 
    setNoticeInfo() {
        let title = Control.state.title;
        switch (title) {
            case "源代码":
                this.value = 'achievements-sourcecode';
                break;
            case "可执行文件":
                this.value = 'achievements-execute';
                break;
            case "支持环境文件":
                this.value = 'achievements-environmentsupport';
                break;
            case "安装部署文件":
                this.value = 'achievements-installation';
                break;
            case "日常成果文件":
                this.value = 'achievements-dailyresults';
                break;
            case "节点成果文件":
                this.value = 'achievements-noderesults';
                break;
        }
    }
    handleTextChange(name, newValue) {
        if (name == "proName") {
            let obj = this.state.proNameAll[newValue.value];
            this.setState({
                proNum: this.state.proNumAll[newValue.value], nodeList: nodeList[obj.proj_category],
                folder_id: this.state.proNameAll[newValue.value]["folder_id"]
            }, () => {
                this.process_id = this.state.proNameAll[newValue.value]["process_id"];
                this.proj_webhook = this.state.proNameAll[newValue.value]["proj_webhook"];
            })
        }
        this.setState({
            [name]: newValue
        });
    }

    handleTimeChange(name, newValue) {
        this.setState({
            [name]: newValue,
            [name + '1']: new Date(newValue).toLocaleDateString(),
        });
    }
    //提交按钮
    handleClick() {
        if (!this.state.achieveName) {
            Toast.show({
                type: 'error',
                content: `成果名称不能为空`
            });
            return;
        } else if (!this.state.finishTime) {
            Toast.show({
                type: 'error',
                content: `成果完成时间不能为空`
            });
            return;
        } else if (!this.state.initSubmit) {
            Toast.show({
                type: 'error',
                content: `是否初次提交不能为空`
            });
            return;
        } else if (!this.state.proName) {
            Toast.show({
                type: 'error',
                content: `所属项目名称不能为空`
            });
            return;
        } else if (!this.state.node) {
            Toast.show({
                type: 'error',
                content: `所属节点不能为空`
            });
            return;
        } else if (!this.state.path_name) {
            Toast.show({
                type: 'error',
                content: `文件存储区域不能为空`
            });
            return;
        } else if (!this.refs.fileselect.fileData || this.refs.fileselect.fileData.length == 0) {
            Toast.show({
                type: 'error',
                content: `附件不能为空`
            });
            return;
        } else if (!this.state['approvers'] || this.state['approvers'].length == 0) {
            Toast.show({
                type: 'error',
                content: `审批人不能为空`
            });
            return;
        }
        if (this.props.pagedata) {
            Actions.submit({ node: this.data, data: this.state, upfileName: this.refs.fileselect.fileData, process_id: this.process_id, proj_webhook: this.proj_webhook, edit: this.state.edit, pagedata: this.props.pagedata });
        } else {
            // debugger
            Actions.submit({ node: this.data, data: this.state, upfileName: this.refs.fileselect.fileData, process_id: this.process_id, proj_webhook: this.proj_webhook });
        }
    }

    handleAddpersonCb(names) {
        this.setState({ approvers: names })
    }
    handleCCpersonCb(names) {
        this.setState({ copyers: names })
    }
    handleAddpersonUnionid(unionids) {
        this.setState({
            approvers_id: unionids
        })
    }
    handlecsrUnionid(unionids) {
        this.setState({
            cc_list_id: unionids
        })
    }
    setPathName(path_name) {
        this.setState({ path_name })
    }
    //获取文件组件数据
    getFileData(data) {
        // this.setState({
        //     upfileName: data
        // })
    }

    render() {
        let t = this;
        console.log(t.state)
        return (
            <div className="achievementmanage myform">
                <Group.List >
                    <TextField
                        label="成果名称"
                        placeholder="请输入成果名称"
                        value={t.state.achieveName}
                        onChange={t.handleTextChange.bind(t, 'achieveName')}
                        readOnly={t.props.edit}
                    />
                </Group.List>
                <Group.List >
                    <DatetimeField
                        label="成果完成时间"
                        placeholder="请选择"
                        columns={['年', '月', '日']}
                        className="time"
                        value={this.state.finishTime}
                        onSelect={this.handleTimeChange.bind(t, 'finishTime')}
                        readOnly={t.props.edit}
                    />
                </Group.List>
                <Group.List >
                    <SelectField
                        label="是否初次提交"
                        placeholder="请选择"
                        value={t.state.initSubmit}
                        options={t.state.options}
                        onSelect={t.handleTextChange.bind(t, 'initSubmit')}
                        readOnly={t.props.edit}
                    />
                </Group.List>
                <Group.List >
                    <SelectField
                        label="所属项目名称"
                        placeholder="请选择"
                        options={t.state.proNameAll}
                        value={t.state.proName}
                        onSelect={t.handleTextChange.bind(t, 'proName')}
                        readOnly={t.props.edit}
                    />
                    <TextField
                        label="所属项目编号"
                        value={t.state.proNum}
                        onChange={t.handleTextChange.bind(t, 'proNum')}
                        readOnly={true}
                    />
                </Group.List>
                <Group.List >
                    <SelectField
                        label="所属节点"
                        placeholder="请选择"
                        options={this.state.nodeList}
                        value={t.state.node}
                        onSelect={t.handleTextChange.bind(t, 'node')}
                        readOnly={t.props.edit}
                    />
                </Group.List>
                <Fileselect
                    ref='fileselect'
                    upfileName={this.state.upfileName}
                    proName={this.state.proName}
                    edit={this.props.edit}
                    folder_id={this.state.folder_id}
                    process_folder={true}//是不是需要审批通过才存储文件
                    pathdata={this.state.pathdata}
                    path_name={this.state.path_name}
                    setPathName={this.setPathName.bind(this)}
                    getFileData={this.getFileData.bind(this)}
                    message={this.value}
                />
                <Group.List>
                    <Addpersonfield
                        label="监理 -- 甲方"
                        cb={t.handleAddpersonCb.bind(t)}
                        approvers={this.state.approvers}
                        union={t.handleAddpersonUnionid.bind(t)}
                        approvers={t.state.approvers}
                        approvers_id={t.state.approvers_id}
                        maxLength="10" />
                </Group.List>
                <Group.List>
                    <Addpersonfield
                        label="抄送人"
                        cb={t.handleCCpersonCb.bind(t)}
                        union={t.handlecsrUnionid.bind(t)}
                        approvers={t.state.cc_list}
                        approvers_id={t.state.cc_list_id}
                        maxLength="10"
                    />
                </Group.List>
                <Button type="primary" className="submitBtn" onClick={t.handleClick.bind(t)}>提交</Button>
            </div>
        );
    }

    componentWillMount() {

    }

    componentDidMount() {
        Actions.fetch();
    }

    componentWillReceiveProps(nextProps) {
        // debugger
        if (nextProps.pagedata.approvers && nextProps.pagedata.approvers instanceof Array)
            this.setState({
                approvers: nextProps.pagedata.approvers,
                approvers_id: nextProps.pagedata.approvers_id,
            });
        else if (nextProps.pagedata.approvers && typeof nextProps.pagedata.approvers == 'string')
            this.setState({
                approvers: [nextProps.pagedata.approvers],
                approvers_id: [nextProps.pagedata.approvers_id]
            });
        if (nextProps.pagedata.cc_list && nextProps.pagedata.cc_list instanceof Array)
            this.setState({
                cc_list: nextProps.pagedata.cc_list,
                cc_list_id: nextProps.pagedata.cc_list_id
            });
        else if (nextProps.pagedata.cc_list && typeof nextProps.pagedata.cc_list == 'string')
            this.setState({
                cc_list: [nextProps.pagedata.cc_list],
                cc_list_id: [nextProps.pagedata.cc_list_id]
            })
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {
    }

    componentWillUnmount() {
    }
}

reactMixin.onClass(Achievementmanage, Reflux.connect(Store));

module.exports = Achievementmanage;
